package com.foreveross.fetl.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.foreveross.fetl.common.SysConstant;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
  
/*
 * json日期值处理器
 */
public class JsonDateValueProcessor implements JsonValueProcessor {

	private String format = SysConstant.DATETIMES_PATTERN;

	public JsonDateValueProcessor() {

	}

	public JsonDateValueProcessor(String format) {
		this.format = format;
	}

	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return process(value, jsonConfig);
	}

	public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
		return process(value, jsonConfig);
	}
	
	private Object process( Object value, JsonConfig jsonConfig ) {
		if (value instanceof Date) {
			String str = new SimpleDateFormat(format).format((Date) value);
			return str;
		}
		return value == null ? null : value.toString();
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

}
